import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Link } from 'react-router'
import { Button, Spin, Form, Input, Table,  Modal, Select,message } from 'antd'
import moment from 'moment';
/* 引用接口文档路径 */
import {
  subjectSelect,subjectInfo,addSubject,deleteSubject
} from 'actions/materialManage'
import '../../style/userControl.less'
import ModalAddSubject from './addSubjectModal.js';

const FormItem = Form.Item
@Form.create({})
//abc
@connect(
  (state, props) => ({
    config: state.config,
    userInfoList: state.userInfoList,
  })
)
export default class app extends Component {
  constructor(props) {
    super(props)
    this.state = {
      list:"",//基础数据列表
      contentModal:false,//列表内容详情弹窗
      modalList:"",//列表内容详情
      addModalFlg:false,//新增专题弹窗
      addModalType:"",
    }
    this.keyword={"name":"",};//搜索关键词
    this.currentNumber = 1;//当前页面
    this.pagesize = 10;//每页条数
  }
  componentDidMount() {
    this.selectList();
  }
  
  selectFn(){//点击搜索按钮
    this.keyword = {
      "name":this.props.form.getFieldValue('name'),
    };
    this.currentNumber=1;
    this.selectList();
  }
  selectList(pageNumber){//查询列表
    const datajson = Object.assign({"start":pageNumber||1,"end":this.pagesize}, this.keyword);
    this.currentNumber = pageNumber||1;
    this.props.dispatch(subjectSelect(
      datajson,
      (respose) => {
        this.setState({
          list: respose,
        })
    }))
  }
  


  addReport(type,id){//新增专题
    //编辑的情况  带数据去组件
    if(type == 2){
      this.props.dispatch(subjectInfo({"id":id},
      (respose) => {
          this.setState({
          addModalFlg: true,
          modalList:respose,
          addModalType:type,
        })
      }))
    }else{//新增的情况
      this.setState({
        addModalFlg: true,
        modalList:{},
        addModalType:type,
      })
    }
  }

  addModalCancel(){//关闭新增专题弹窗
    this.setState({
      addModalFlg: false,
    })
  }

  contentModalCancel(){//关闭查看弹窗
    this.setState({
      contentModal: false,
    },()=>{
      this.setState({
        modalList: "",
      })
    })
  }

  showModal(id){//点击查看  专题详情
    this.props.dispatch(subjectInfo({"id":id},
      (respose) => {
        this.setState({
          contentModal: true,
          modalList:respose
        })

    }))
  }

  deleteList(id){
    Modal.confirm({
      content:"确定要删除该条信息么？",
      onOk:()=>{
        this.props.dispatch(deleteSubject(
          {"id":id},
          (respose) => {
            var length = this.state.list.respData.count ? this.state.list.respData.count : 0;
            if(Math.ceil(length/this.pagesize) == this.currentNumber && length%this.pagesize == 1 && this.currentNumber != 1){
              this.currentNumber = this.currentNumber - 1;
            }
            message.success(respose.respMsg);
            this.contentModalCancel();
            this.selectList(this.currentNumber);
        }))
      },
      onCancel:()=>{

      }
    })
  }



  columns() {//表格标题
    return [
      {
        title: '专题编号',
        key: 'code',
        dataIndex: 'code',
        width: '10%',
      },
      {
        title: '专题名称',
        dataIndex: 'name',
        key: 'name',
        width: '10%',
      },
      {
        title: '封面图',
        dataIndex: 'coverPath',
        key: 'coverPath',
        width: '10%',
        render: (text, recordId, index) => <img style={{width:"80px"}} src={text}/>,
      },
      {
        title: '专题简介',
        dataIndex: 'introduction',
        key: 'introduction',
        width: '10%',
        render: (text, recordId, index) => <div dangerouslySetInnerHTML={{__html: text}} ></div>,
      },
      {
        title: '报告数',
        dataIndex: 'reportTotal',
        key: 'reportTotal',
        width: '10%',
      },
      {
        title: '操作',
        width: '10%',
        render: (text, recordId, index) => 
        <div>
          <Button type="primary" size="small" onClick={this.showModal.bind(this,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>查看</Button>          
          <Button type="primary" size="small" onClick={this.addReport.bind(this,2,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>编辑</Button>  
        </div>,
        
      },
    ]
  }
  


  render () {
    const {form } = this.props
    const { getFieldDecorator } = form    
    const respData = this.state.list.respData ? this.state.list.respData : [];//刷新页面调用接口数据赋值
    const modalList = this.state.modalList.respData ? this.state.modalList.respData :[];
    const dateFormat = 'YYYY/MM/DD';
    return (
      <div className="page">
        {/* 搜索条件筛选块 */}
        <div className="searcDiv">
          <Form onSubmit={this.selectFn.bind(this)} layout="inline">
            <FormItem label="专题名称">
              {
                getFieldDecorator('name', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <Input placeholder="请输入关键字" size="default" style={{ width: '200px' }} />
                )
              }  
            </FormItem>
            <Button type="primary" onClick={this.selectFn.bind(this)}>搜索</Button>
            <Button type="primary" onClick={this.addReport.bind(this,1)}>新增</Button>
          </Form>
        </div>
        {/*数据渲染列表*/}
        <Spin spinning={false}>
          <Table
            rowKey="id" 
            columns={this.columns()}//表格标题列
            dataSource={respData.resultData}
            pagination={{ total: respData.count, current:this.currentNumber,pageSize: this.pagesize, onChange: this.selectList.bind(this)}}
            scroll={{ y: true }}
          />
        </Spin>
        {/* 新增专题弹窗 */}
        {
          this.state.addModalFlg ? <ModalAddSubject
          type={this.state.addModalType}
          ModalFlg={this.state.addModalFlg} 
          ModalCancel={this.addModalCancel.bind(this)} 
          callback={this.selectList.bind(this,this.state.addModalType==1?1:this.currentNumber)}
          selectTypeList={this.state.selectTypeList}
          content={this.state.modalList.respData ? this.state.modalList.respData : {}}
          /> : ''
        }
        {/* 信息展示弹窗 */}
        <Modal
          width={"80%"}
          visible={this.state.contentModal}
          title="专题详情"
          onCancel={this.contentModalCancel.bind(this)}
          footer={[
            <Button key="submit" type="primary"  onClick={this.deleteList.bind(this,modalList.id)}>删除</Button>,
          ]}
        >
          <ul className="joinBlackListModal">
            <li>
              <span className="title">专题名称:</span><span className="content">{modalList.name}</span>
            </li>
            <li>
              <span className="title">专题封面:</span><span className="content"><img style={{width:"80px"}} src={modalList.coverPath?modalList.coverPath:require('../../images/baseImg.png')}/></span>
            </li>
            <li>
              <span className="title">专题简介:</span><span className="content" >{modalList.introduction}</span>
            </li>
            <li>
              <span className="title">专题内容:</span><span className="content" dangerouslySetInnerHTML={{__html: modalList.content}}></span>
            </li>
            <li>
              <span className="title">专题报告:</span>
              <span className="content">
                {
                  (modalList.reportInfoList ? modalList.reportInfoList :[]).map((item,i)=>{
                      return(
                        <p key={i}>
                          <span style={{marginRight:"5px"}}>{item.title}</span>
                          <span>{item.publisherName}</span>                          
                        </p>
                      )
                  })
                }
              </span>
            </li>
          </ul>
        </Modal>
      </div>
    )
  }
}
